package com.bloveoacom.weichi.fragment;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bloveoacom.base.AppContext;
import com.bloveoacom.base.BaseList2Fragment;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.DataUtil;
import com.bloveoacom.util.DialogUtil;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.util.NumberUtil;
import com.bloveoacom.util.PicCameraLocalUtil;
import com.bloveoacom.util.ShareUtils;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.activity.PostDetail2Activity;
import com.bloveoacom.weichi.activity.PostDetailActivity;
import com.bloveoacom.weichi.bean.NewsPostBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import timber.log.Timber;

import static com.bloveoacom.util.DataUtil.setItemView;
import static com.bloveoacom.util.PicCameraLocalUtil.recycle;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/25
 * desc  :
 */
public class WaitChecktFragment extends BaseList2Fragment<NewsPostBean> {

    @BindView(R.id.tv_post_id)
    TextView tvPostId;
    @BindView(R.id.tv_post_title)
    TextView tvPostTitle;
    @BindView(R.id.layout_share)
    ConstraintLayout layoutShare;
    public static WaitChecktFragment newInstance() {
        Bundle args = new Bundle();
        WaitChecktFragment fragment = new WaitChecktFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.rv_item_post2;
    }

    @Override
    protected void convertView(BaseViewHolder helper, NewsPostBean item) {
        DataUtil.setItemView2(mContext,helper,item,1);
        helper.setVisible(R.id.group2,true);
        helper.setVisible(R.id.group,false);
        ProgressBar view1 = helper.getView(R.id.id_seekbar_1);
        ProgressBar view2 = helper.getView(R.id.id_seekbar_2);
        view1.setProgress(item.supportNum);
        view2.setProgress(item.againstNum);
        helper.setText(R.id.tv_no_agreen,item.againstNum+"/21");
        helper.setText(R.id.tv_support,item.supportNum+"/80");
        helper.addOnClickListener(R.id.iv_share);

    }

    @Override
    protected void getData() {
        BaseSubscriber2<Object> baseSubscriber2 = HttpMethods.getInstance().getHttpService().findUserCoinList(UserInfoBean.getInstance().getToken(), "1", page, getPageSize()).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(this) {
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                String s = GsonUtil.GsonString(o);
                List<NewsPostBean> datas = GsonUtil.GsonToList(s, NewsPostBean.class);
                doData(datas);
            }
        });
        addDispose(baseSubscriber2);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh_share;
    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ActivityUtil.startActivity(mContext, PostDetail2Activity.class, datas.get(position).coinId);
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                NewsPostBean newsPostBean = datas.get(position);
                tvPostId.setText("文章ID:"+newsPostBean.coinId);
                tvPostTitle.setText(newsPostBean.getTitle());
                share();
            }
        });
    }

    Dialog dialog;
    Bitmap bitmap;
    File sharefile;
    private void share() {
        AppContext.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bitmap = PicCameraLocalUtil.getBitmap(layoutShare);
                sharefile = PicCameraLocalUtil.saveBitmap(mContext, bitmap, "dsh_post2");
                dialog = DialogUtil.getPicBottomDialog2(mContext, new DialogUtil.PhotoDoWhat() {
                    @Override
                    public void doone() {
                        ShareUtils.shareFriends(sharefile.getPath(), 0);
                        DialogUtil.dissDialog(dialog);
                        ToastUtil.makeShortText(mContext, "分享操作正在后台进行");
                    }

                    @Override
                    public void dotwo() {
                        ShareUtils.shareFriends(sharefile.getPath(), 1);
                        DialogUtil.dissDialog(dialog);
                        ToastUtil.makeShortText(mContext, "分享操作正在后台进行");
                    }

                    @Override
                    public void dothree() {

                    }
                }, sharefile);
                DialogUtil.showDialog(dialog);
            }
        }, 1000);
    }
    @Override
    public void onDestroy() {
        recycle(bitmap);
        super.onDestroy();
    }
}
